package com.xu.cloud.codegenerator.controller;

import com.xu.cloud.codegenerator.pojo.http.req.CodeGenerateReq;
import com.xu.cloud.codegenerator.pojo.http.req.QueryMultipleTablesReq;
import com.xu.cloud.codegenerator.pojo.http.resp.QueryMultipleTablesResp;
import com.xu.cloud.codegenerator.service.GeneratorService;
import com.xu.cloud.common.pojo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;

/**
 * @author xuguan
 * @since 2024/1/7
 */
@Api(tags = "代码生成器Controller")
@RestController
@RequestMapping("/sys/generator")
@RequiredArgsConstructor
@Validated
public class GeneratorController {

    private final GeneratorService generatorService;

    @ApiOperation(value = "queryMultipleTables", notes = "通过表名模糊查询表结构信息, 或查询当前数据源下所有表结构信息")
    @GetMapping("/list")
    public Result<QueryMultipleTablesResp> queryMultipleTables(QueryMultipleTablesReq queryMultipleTablesReq) {
        return Result.ok(generatorService.queryMultipleTables(queryMultipleTablesReq));
    }

    /**
     * 生成代码
     * TODO 改造成前后端分离模式, 使用 {@link com.xu.cloud.codegenerator.pojo.http.resp.CodeGenerateResp}
     */
    @ApiOperation(value = "code", notes = "生成代码, 下载code.zip压缩包文件")
    @GetMapping("/code")
    public void code(@Valid CodeGenerateReq codeGenerateReq) throws IOException {
        generatorService.generatorCode(codeGenerateReq.getTableNames().split(","));
        // final byte[] data = generatorService.generatorCode(codeGenerateReq.getTableNames().split(","));
        // HttpServletUtil.downLoad("code.zip", data);
    }
}
